package com.szj.spushrtmp.push;

import com.szj.spushrtmp.util.audio.AudioReadListener;
import com.szj.spushrtmp.util.audio.AudioRecorderManager;

/**
 * @ClassName: AudioChannel
 * @Author: s10g
 * @CreateDate: 7/30/23$ 12:27 PM$
 * TODO
 */
public class AudioChannel implements AudioReadListener {

    private final AudioRecorderManager recorderManager;

    // 通道数
    public static final int CHANNEL = 2;

    // 采样率
    public static final int SAMPLE_RATE = 44100;

    public AudioChannel() {
        // 初始化faac编码器
        init();

        recorderManager = new AudioRecorderManager(CHANNEL, SAMPLE_RATE, getInputSample() * CHANNEL);

        recorderManager.setAudioReadListener(this);
    }

    private void init() {
        native_initFaacEncoder(CHANNEL, SAMPLE_RATE);
    }

    public void start() {
        recorderManager.start();
    }

    public void stop() {
        recorderManager.stop();
    }

    public void release() {
        recorderManager.release();
    }

    /*
     * 作者:s10g
     * 创建时间: 8/3/23 1:39 PM
     * TODO 音频数据回调
     */
    @Override
    public void result(byte[] data, int len) {
        native_AudioData(data, len);
    }


    /*
     * 作者:s10g
     * 创建时间: 8/3/23 1:13 PM
     * TODO 初始化faac
     */
    public native void native_initFaacEncoder(int channel, int sampleRate);

    /*
     * 作者:s10g
     * 创建时间: 8/3/23 1:45 PM
     * TODO 获取输出样本数 [初始化faac后调用]
     */
    public native int getInputSample();

    /*
     * 作者:s10g
     * 创建时间: 8/3/23 1:44 PM
     * TODO 音频输出监听
     */
    public native void native_AudioData(byte[] data, int len);
}
